<?php
/**
 * Created by PhpStorm.
 * User: jochen
 * Date: 2016/12/23
 * Time: 下午6:32
 */

namespace JC\CodeWars;


class BaseQues
{
    public function sumNumberFrom0ToN($last) {
        if ($last < 0) {
            return "$last < 0";
        }
        if ($last === 0) {
            return '0=0';
        }
        $list = range(0, $last, 1);
        $exp = join('+', $list);
        $result = array_sum($list);
        return "$exp = $result";
    }

    /**
     * Bit Counting
     * @param int $number
     * @return int
     */
    public function bitCounting(int $number)
    {
        assert(is_numeric($number) && $number > 0);
        return substr_count(decbin($number), '1');
    }

    /**
     * Max Rot
     * @param int $n
     * @return int
     * Take a number: 56789. Rotate left, you get 67895.
     * Keep the first digit in place and rotate left the other digits: 68957.
     *
     * Keep the first two digits in place and rotate the other ones: 68579.
     *
     * Keep the first three digits and rotate left the rest: 68597. Now it is over since keeping the first four it remains only one digit which rotated is itself.
     * You have the following sequence of numbers:
     * 56789 -> 67895 -> 68957 -> 68579 -> 68597
     *
     * and you must return the greatest: 68957
     *
     * max_rot(56789) should return 68957
     */
    public function maxRot($n)
    {
        return max(array_reduce(range(0, strlen($n)), function($r, $i) {
            $last = $r[0];
            $strNumber = substr($last, 0, $i) . substr($last, $i+1) . substr($last, $i, 1);
            array_unshift($r, intval($strNumber));
            return $r;
        }, [$n]));
    }

    /**
     * @param $s
     * @return string
     * This time no story, no theory. The examples below show you how to write function accum:
     *   Examples:
     *   accum("abcd");    // "A-Bb-Ccc-Dddd"
     *   accum("RqaEzty"); // "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
     *   accum("cwAt");    // "C-Ww-Aaa-Tttt"
     *   The parameter of accum is a string which includes only letters from a..z and A..Z.
     */
    public function accum($s)
    {
        $charList = str_split($s);
        array_walk($charList, function(&$v, $i) {
            $v = ucfirst(str_repeat(strtolower($v), ++$i));
        });
        return implode('-', $charList);
    }
}